<?php
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$_W['page']['title'] = '核销管理';

// TODO 添加后台核销员字段
if (!pdo_fieldexists('tg_hexiao', 'check_user')) {
    pdo_query("ALTER TABLE " . tablename('tg_hexiao') . " ADD `check_user` varchar(50) COMMENT '后台核销员';");
}

// TODO 添加核销卡券字段
if (!pdo_fieldexists('tg_hexiao', 'favorable_id')) {
    pdo_query("ALTER TABLE " . tablename('tg_hexiao') . " ADD `favorable_id` int(10) COMMENT '卡券id';");
}

// TODO 添加核销卡券字段
if (!pdo_fieldexists('tg_hexiao', 'favorabler_id')) {
    pdo_query("ALTER TABLE " . tablename('tg_hexiao') . " ADD `favorabler_id` int(10) COMMENT '卡券拥有者id';");
}

if ($op == 'display') {

} elseif ($op == 'post') {

    $orderno = $_GPC['orderno'];
    $openid = $_GPC['openid'];
    $checkorder = pdo_fetch("select * from " . tablename('tg_collect') . " where  orderno = '{$orderno}' ");
    $is_hexiao_member = 1;
    $order = pdo_fetch('select * from ' . tablename('tg_order') . ' where orderno = :orderno', array(':orderno' => $checkorder['orderno']));
//    die(json_encode(array('order' => $order , 'check' => $checkorder)));

    if ($checkorder['active_times'] < 1) {
        message("该项目已无服务次数，请勿重复服务！", referer(), "error");
    }
    if (pdo_update('tg_collect', array('active_times' => ($checkorder['active_times'] - 1)), array('id' => $checkorder['id']))) {
        $data = array();
        $data['uniacid'] = $checkorder['uniacid'];
        $data['cid'] = $checkorder['id'];
        $data['orderno'] = $checkorder['orderno'];
        $data['openid'] = $checkorder['openid'];
        $data['check_user'] = $_W['user']['username'];
        $data['check_time'] = TIMESTAMP;
        pdo_insert('tg_hexiao', $data);
        if ($checkorder['active_times'] == 1) {
            pdo_update('tg_collect', array('check_status' => 1), array('id' => $checkorder['id']));
        }

        $check_order = pdo_fetchall("select * from " . tablename('tg_collect') . " where  orderno = :orderno  and check_status = 0", array(':orderno' => $checkorder['orderno']));
        if (count($check_order) == 0) {
            pdo_update('tg_order', array('status' => 3, 'successtime' => TIMESTAMP), array('orderno' => $checkorder['orderno']));
        }
        pdo_update('tg_member' , array('order_times +=' => 1) , array('uniacid' => $checkorder['uniacid'] , 'openid' => $checkorder['openid']));
        message('核销成功！', referer(), 'success');
    } else {
        message('核销失败，请重试！', referer(), 'error');
    }
    include wl_template('order/hexiao');

}elseif ($op == 'coupon'){

    $id = $_GPC['id'];
    $openid = $_GPC['openid'];
//    die(json_encode(array('id' => $id , 'openid' => $openid)));
    $checkorder = pdo_fetch("select * from".tablename('tg_favorabler')." where  id = '{$id}' ");

    $sql = "SELECT * FROM " . tablename('tg_favorable') . " where id = :id";
    $list = pdo_fetch($sql, array(':id' => $checkorder['favorable_id']));
    $is_hexiao_member=1;

    if($checkorder['times'] < 1)
    {
        message("该卡券核销次数已用完，请勿重复核销！", referer(), "error");
    }
    if(pdo_update('tg_favorabler',array('times' => ($checkorder['times'] - 1)),array('id' => $id))){
        $data = array();
        $data['uniacid'] = $checkorder['uniacid'];
        $data['favorabler_id'] = $checkorder['id'];
        $data['favorable_id'] = $checkorder['favorable_id'];
        $data['openid'] = $checkorder['openid'];
        $data['check_user'] = $_W['user']['username'];
        $data['check_time'] = TIMESTAMP;
        pdo_insert('tg_hexiao', $data);
        if ($checkorder['times'] == 1){
            pdo_update('tg_favorabler',array('status' => 1),array('id' => $id));
        }
        pdo_update('tg_member' , array('favorable_times +=' => 1) , array('uniacid' => $checkorder['uniacid'] , 'openid' => $checkorder['openid']));
        message('核销成功！', referer(), 'success');
    }else{
        message('核销失败，请重试！', referer(), 'error');
    }

} else {
    message('请求方式不存在');
}
exit();